<?php
/**
 * 考勤
 * 请假，
 * 张名尊， 20140429
 */ 


class leaveAction extends backendAction{
    
    public function _initialize() {
        parent::_initialize();
		
		$_REQUEST['sort'] = 'createTime';
		
        $this->_mod = D('leave');
       $this->_cate_mod = D('region');
    }   
    
    public function _before_index(){
        $this->list_relation = true;
        //$a = D('admin')->get_user_info(1);
        //print_r($a);
        //setUserSession($a);
        $this->readyData();
        
    }
    
    
    protected function _search() {
        $map = array();
        ($time_start = $this->_request('time_start', 'trim')) && $map['createTime'][] = array('egt', strtotime($time_start));
        ($time_end = $this->_request('time_end', 'trim')) && $map['createTime'][] = array('elt', strtotime($time_end)+(24*60*60-1));
        ($verifyType = $this->_request('verifyType', 'trim')) && $map['verifyType'] = $verifyType;   
        ($type = $this->_request('type', 'trim')) && $map['type'] = $type;       
        $userName = $this->_request('userName', 'trim');
        if($userName)
        {
            $where['userName'] = array('like', '%'.$userName.'%');           
            $where['realName'] = array('like', '%'.$userName.'%');
            $where['_logic'] = 'or';
            $userMap['_complex'] = $where;                         
        }
        
        //从厅店取用户
        $regionId = $this->_request('regionId');
        $selected_ids = '';
        if ($regionId) {
            $regionArr = explode('_',$regionId);
            if(count($regionArr)==2){
                  $regionId = $regionArr[0];
                  $storeId   = $regionArr[1];  
                  $userMap['storeId']  = $storeId ;
                  $spid = $this->_cate_mod->where(array('id'=>$regionId))->getField('spid');
                  $selected_ids = $spid ? $spid . $regionId .'|'.$regionId.'_'.$storeId: $regionId.'|'.$regionId.'_'.$storeId;
            }else{
                $id_arr = $this->_cate_mod->get_child_ids($regionId, true);
                $userMap['regionId'] = array('in',$id_arr);                
                $spid = $this->_cate_mod->where(array('id'=>$regionId))->getField('spid');
                $selected_ids = $spid ? $spid . $regionId : $regionId;
            }          
        }            
        if(!empty($userMap)){
           
            $userList = M('admin')->where($userMap)->getField('id,userName');
                        
            $userIds = array_keys($userList);
            if(!empty($userIds)){
                $map['userId'] = array('in',$userIds);
            }  
        }
        
		$admin = getUserSession();
        if(!empty($admin['regionId'])){
            //$this->_cate_mod = D('region');
            $id_arr = D('region')->get_child_ids($admin['regionId'], true);
            $id_arr = M('admin')->where(array('regionId'=>array('in',$id_arr)))->getField('id,userName');

            $id_arr = array_keys($id_arr);   
            if(!empty($id_arr))
            {         
                $map['userId'] = array('in',$id_arr);
            }
        }
              
      
      
        $this->assign('search', array(
            'time_start' => $time_start,
            'time_end' => $time_end,
            'type'  => $type,
            'verifyType' => $verifyType,
            'selected_ids' => $selected_ids,      
            'userName'=>$userName,            
        ));
        return $map;
    }    
    
    
    
    private function readyData(){
        //考勤审核类型
        $checkVerifyType = C('CHECK_VERIFY_TYPE');
        //请假类型
        $leaveType = C('LEAVE_TYPE');
        //请假审批类型
        $leaveVerifyType = C('LEAVE_VERIFY_TYPE');
        $this->assign('leaveVerifyType',$leaveVerifyType);
        $this->assign('leaveType',$leaveType);
        $this->assign('checkVerifyType',$checkVerifyType);
       
           
    }
    
	/**
	 * 审核考勤
	 */
	public function verify(){
		$leaveId = I('leaveId');
		
		//请假类型
        $leaveType = C('LEAVE_TYPE');
		//请假审批类型
        $leaveVerifyType = C('LEAVE_VERIFY_TYPE');
		
		$field = array(
			'l.*',
			'admin.userName' => 'userName',
			'store.name' => 'storeName',
		);

		$data = $this->_mod->alias('l')->field($field)
				->join(C('DB_PREFIX')."admin AS admin ON admin.id = l.userId")
				->join(C('DB_PREFIX')."store AS store ON store.id = admin.storeId")
				->where(array('l.id'=>$leaveId))->find();
		
		$data['leaveType'] = $leaveType[$data['type']];
		
		$this->assign('data', $data);
		$this->assign('leaveVerifyType', $leaveVerifyType);
        $response = $this->fetch();
        $this->ajaxReturn(1, '', $response);
		
	}
	
	/**
	 * 假期审核结果提交
	 */
	public function verifySubmit(){
		$leaveId = I('leaveId');
		
		$data['verifyType'] = I('verifyTypeId');	//审核结果类型
		$data['verifyRemark'] = I('verifyRemark');	//审核结果备注
		$data['verifyTime'] = time();
		
		$response = $this->fetch('leave:verify');
		
		if(! $data['verifyType']){
			$this->ajaxReturn(0, '请选择审核结果', $response);
		}
		
		if(! $data['verifyRemark']){
			$this->ajaxReturn(0, '请填写审核备注', $response);
		}
		
		if( $leaveId && D('leave')->where(array('id'=>$leaveId))->save($data) ){
            $this->ajaxReturn(1, '', '', 'verify');
		}else{
            $this->ajaxReturn(0, '', $response);
		}
	}    
                
}